# -*- coding: utf-8 -*-
from PyQt4 import QtCore , QtGui
from assetPublish_UI import assetPublish_UI
from lib.papyDB import papyDB , tableInfo
from lib import globalVar as GV
from lib.papyDBTable import  *
import os
try :
    import maya.cmds as cmds
    import sys
    sys.path.append('C:\Python26\Lib\site-packages')
    sys.path.append('D:\kali\General\papyrus\py')
    import MySQLdb
    MAYA = 1
except :
    MAYA = 0

    

class cAssetPublish( assetPublish_UI ):
    def __init__(self):
        assetPublish_UI.__init__(self)
        self.setUI()
        self.cancel_btn.clicked.connect( self.close )       
        self.assetPub_btn.clicked.connect( self.doPublish )
        

    def doPublish(self):
        if GV.testUserID == '': self.userID = USER_INFO.search('userID' , userLogInName = os.environ['username'])
        else :                  self.userID = GV.testUserID
#        self.userID = papyDB.getFetch('select userID from USER_INFO where userLogInName = "%s"' % os.environ["username"])[0][0]
        self.userName = USER_INFO.search( 'userLogInName' , userID=self.userID )
        self.deptID = USER_INFO.search( 'deptID' , userID=self.userID )
        self.prjName = str( self.prj_cb.currentText() )
        self.seqName = str( self.seq_cb.currentText() )

        self.shotName = str( self.shot_cb.currentText() )        
        self.assetName = str( self.asset_cb.currentText() )
        self.assetID = ASSET.search("assetID" , assetName=self.assetName)          
        self.comment = str( self.cmmt_txt.toPlainText() )     
#        print self.prjName , self.seqName ,  self.shotName , self.assetName , self.comment
        self.assetJobID = ASSET_JOB.search("ID" , assetID=self.assetID , userID=self.userID) 
        self.verCnt = papyDB.getLastID("ASSET_JOB_VERSION", "jobVerID") 

        if self.assetJobID != []:
            self.assetJobVer = ASSET_JOB_VERSION.search("ver" , fID=self.assetJobID)
            if self.assetJobVer != [] :
                self.assetJobVer = self.assetJobVer[len(self.assetJobVer)-1]+1 if type(self.assetJobVer)==type([]) else self.assetJobVer+1
            else :
                self.assetJobVer = 1
                
        self.saveAsset()        
                
        self.regAssetVer()
        
        if len(self.comment) > 0 :
            self.regAssetCmt()
        self.setStatus()
        
        self.alertWin("Notice" , "Done!")
        
       
                
    def regAssetVer(self):
        s = os.sep 
        deptInfo = []
        if self.deptID == 2     : deptInfo = ['Model' , 'model']
        elif self.deptID == 4   : deptInfo = ['Rigging' , 'rig']
        elif self.deptID == 5   : deptInfo = ['Texture' , 'tex'] 
        if self.case == 1 :
#            print GV.PROJECT_SERVER +s+s+ self.prjName +s+s+ "VFX" +s+s+ self.seqName +s+s+ self.shotName +s+s+ deptInfo[0] +s+s+ self.newFile
            papyDB.regTable('ASSET_JOB_VERSION' , self.assetJobVer , self.assetJobID, GV.PRJ_ +s+s+ GV.PRJ__ +s+s+ self.prjName +s+s+ "VFX" +s+s+ self.seqName +s+s+ self.shotName +s+s+ deptInfo[0] +s+s+ self.newFile)
        else : 
            papyDB.regTable('ASSET_JOB_VERSION' , self.assetJobVer , self.assetJobID, GV.PRJ_ +s+s+ GV.PRJ__ +s+s+ self.prjName +s+s+ "VFX" +s+s+ "_library" +s+s+ deptInfo[0] +s+s+ self.newFile )
                

    def regAssetCmt(self):        
        papyDB.regTable('ASSET_JOB_CMMT' , self.comment , self.verCnt+1)

    def setStatus(self):        
        ASSET_JOB.update('status' , 'WIP' , assetID=self.assetID , userID=self.userID)   

    def saveAsset(self):
        s = os.sep   
#        if beforeVer != '000' : 
#            filePath = GV.PROJECT_SERVER + s[1] + self.prjName + s[1] + "Dev" + s[1] + deptInfo[0] + s[1] + self.assetName+'_'+deptInfo[1]+'_'+ver+'.ma'
        
        
#        print self.seqName , self.shotName
        self.case = 1
        if self.seqName == '--Master--' and self.shotName == '--Global--' :
            self.case = 0
        if self.shotName == '--Global--' and self.seqName != '--Master--' :
#            self.case = 1
            self.shotName = '_library'
##====================================================================================================================================
        deptInfo = []
        if self.deptID == 2     : deptInfo = ['Model' , 'model']
        elif self.deptID == 4   : deptInfo = ['Rigging' , 'rig']
        elif self.deptID == 5   : deptInfo = ['Texture' , 'tex'] 
        
        defVer = str(self.assetJobVer).zfill(3)        
        ver = str(int(defVer)).zfill(3)
        beforeVer = str(int(defVer) - 1).zfill(3)
        
##====================================================================================================================================  
        self.newFile = self.assetName+'_'+deptInfo[1]+'_'+self.userName+'_'+ver+'.ma'
        if self.case == 0 :
            self.newPath = os.path.join( GV.PROJECT_SERVER , self.prjName , "VFX" , "_library" , deptInfo[0] , self.newFile )
        else :
            self.newPath = os.path.join( GV.PROJECT_SERVER , self.prjName , "VFX" , self.seqName , self.shotName , deptInfo[0] , self.newFile )
#            newPath2 = GV.PROJECT_SERVER +s+ self.prjName +s+ "VFX" +s+ self.seqName +s+ self.shotName +s+ deptInfo[0] +s+ newFile
            
            
            
        if beforeVer != '000' : 
            oldFile = self.assetName+'_'+deptInfo[1]+'_'+self.userName+'_'+beforeVer+'.ma'
            oldPath = os.path.join( GV.PROJECT_SERVER , self.prjName , "VFX" , self.seqName , self.shotName , deptInfo[0] , oldFile )            
        else : 
            oldPath = GV.tempMAYAfile
            
#        print "=====>" , oldPath
#        print "=====>" , self.newPath
        
##====================================================================================================================================
        if MAYA :
#            cmds.file( oldPath , o=True , f=True )
            if self.case == 1 :
                cmds.file( rn=GV.PROJECT_SERVER +s+ self.prjName +s+ "VFX" +s+ self.seqName +s+ self.shotName +s+ deptInfo[0] +s+ self.newFile )
            else :
                cmds.file( rn= GV.PROJECT_SERVER +s+ self.prjName +s+ "VFX" +s+ "_library" +s+ deptInfo[0] +s+ self.newFile )
            cmds.file( s=True , f=True )
        else : 
            os.system("copy %s %s" % (oldPath , self.newPath) )
##====================================================================================================================================


        
#    def closeEvent( self , event ) :        
#        papyDB.cr.close()
        
        
        
        
def theTool( ):
    if MAYA == True :
        global app    
        try:        
            app.close()        
        except :        
            app = cAssetPublish()
        app.show()
    else :
        import sys
        app = QtGui.QApplication(sys.argv)
        mainWin = cAssetPublish()   
        mainWin.show()
        sys.exit(app.exec_())

if __name__ == '__main__':
    theTool()
    
    
    
    
#if __name__ == '__main__':
#    import sys    
#    app = QtGui.QApplication(sys.argv)
#    ui = cAssetPublish()    
#    app.exec_()
    
    